package com.example.demo.service;

import com.example.demo.dto.AreaDto;
import com.example.demo.dto.CommonDto;
import com.example.demo.dto.UpdateAreaDto;
import com.example.demo.entity.Address;
import com.example.demo.mapper.AddressMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Dfrus
 * @Date 2022/2/19 16:37
 **/
@Service
@AllArgsConstructor
public class AddressService {
    private AddressMapper addressMapper;

    public List<Address> findAddress(Integer id){
        return addressMapper.selectAddress(id);
    }

    public void findArea(AreaDto areaDto){
        addressMapper.selectArea(areaDto);
    }

    public Address findOne(Integer id){
        return addressMapper.selectOne(id);
    }

    public void updateFind(UpdateAreaDto updateAreaDto){
        Address address = addressMapper.selectOne(updateAreaDto.getAddrId());
        if (updateAreaDto.getProvince() == null){
            updateAreaDto.setProvince(address.getProvince());
        }
        if (updateAreaDto.getCity() == null){
            updateAreaDto.setCity(address.getCity());
        }
        if (updateAreaDto.getArea() == null){
            updateAreaDto.setArea(address.getArea());
        }
        addressMapper.updateSelect(updateAreaDto);
    }

    public void updateCommon(CommonDto commonDto){
        for (Integer i = 0 ; i < commonDto.getAddrId().length; i++){
            Address address = addressMapper.selectOne(commonDto.getAddrId()[i]);
            if (address.getCommonAddr() == 0){
                address.setCommonAddr(1);
                addressMapper.updateCommon(address);
            }else {
                address.setCommonAddr(0);
                addressMapper.updateCommon(address);
            }

        }

    }
}
